package com.example.shoppingmallspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.shoppingmallspringboot.entity.Orders;
import com.example.shoppingmallspringboot.entity.OrdersDetail;
import com.example.shoppingmallspringboot.entity.Goods;
import com.example.shoppingmallspringboot.mapper.CartMapper;
import com.example.shoppingmallspringboot.mapper.GoodsMapper;
import com.example.shoppingmallspringboot.mapper.OrdersDetailMapper;
import com.example.shoppingmallspringboot.mapper.OrdersMapper;
import com.example.shoppingmallspringboot.service.OrdersService;
import com.example.shoppingmallspringboot.util.FwResult;
import com.example.shoppingmallspringboot.vo.OrdersDetailGoodsVo;
import com.example.shoppingmallspringboot.vo.OrdersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl implements OrdersService {
    
    @Autowired
    private OrdersMapper ordersMapper;
    
    @Autowired
    private GoodsMapper goodsMapper;
    
    @Autowired
    private OrdersDetailMapper ordersDetailMapper;
    
    @Autowired
    private CartMapper cartMapper;
    
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<OrdersDetailGoodsVo> getUserOrders(Integer userId) {
        List<OrdersDetailGoodsVo> ordersDetailGoodsVoList = ordersMapper.getOrdersDetailGoods();
        Map<Integer,List<OrdersDetailGoodsVo>> map = ordersDetailGoodsVoList.stream()
                .collect((Collectors.groupingBy(OrdersDetailGoodsVo::getUser_id)));
        return map.get(userId);
    }

    @Override
    public void orders(List<OrdersVo> ordersVoList) {
        Integer uuid = UUID.randomUUID().toString().replaceAll("-","").hashCode();
        uuid = uuid < 0 ? -uuid : uuid;
        
        Orders orders = new Orders();
        orders.setId(uuid);
        orders.setUserId(ordersVoList.get(0).getUserId());
        orders.setCreateTime(new Date());
        orders.setName(ordersVoList.get(0).getName());
        orders.setPhone(ordersVoList.get(0).getPhone());
        orders.setCity(ordersVoList.get(0).getCity());
        orders.setAddress(ordersVoList.get(0).getAddress());
        ordersMapper.insert(orders);
        
        double sum = 0;
        for (OrdersVo ordersVo : ordersVoList) {
            Goods goods = goodsMapper.selectById(ordersVo.getGoodsId());
            Integer goodsNum = goods.getGoodsNum();
            Double unitPrice = goods.getGoodsSellingPrice();
            
            OrdersDetail ordersDetail = new OrdersDetail();
            ordersDetail.setOrdersId(uuid);
            ordersDetail.setGoodsId(ordersVo.getGoodsId());
            ordersDetail.setUnitPrice(unitPrice);
            ordersDetail.setQuantity(ordersVo.getQuantity());
            
            sum += unitPrice * ordersVo.getQuantity();
            
            ordersDetailMapper.insert(ordersDetail);
            cartMapper.deleteById(ordersVo.getCartId());
            
            UpdateWrapper<Goods> goodsUpdateWrapper = new UpdateWrapper<>();
            goodsUpdateWrapper.eq("goods_id", ordersVo.getGoodsId())
                    .set("goods_num", goodsNum - ordersVo.getQuantity());
            goodsMapper.update(null, goodsUpdateWrapper);
        }
        
        orders.setAmount(sum);
        ordersMapper.updateById(orders);
    }

    @Override
    public FwResult deleteOrder(Integer orderId) {
        if (orderId != null) {
            QueryWrapper<OrdersDetail> ordersDetailQueryWrapper = new QueryWrapper<>();
            ordersDetailQueryWrapper.eq("orders_id", orderId);
            ordersMapper.deleteById(orderId);
            ordersDetailMapper.delete(ordersDetailQueryWrapper);
        }
        return FwResult.ok();
    }
} 